// -*- c++ -*-
/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#ifndef RSBLOCK_H__
#define RSBLOCK_H__

#include <iostream>
#include <string>
#include "CF__Resource_impl.h"
#include "AudioPort_impl.h"
#include "SceOsSemaphore.h"
#include "SceOsThreadManager.h"
#include "mc_error.h"
#include "mac_port.h"

#include "GaloisField.h"
#include "GaloisFieldElement.h" 
#include "GaloisFieldPolynomial.h"

#ifdef _WIN32
#ifdef RSBLOCK_EXPORTS
#define RSBLOCK_EXPORT __declspec(dllexport)
#else
#define RSBLOCK_EXPORT __declspec(dllimport)
#endif
#else
#define RSBLOCK_EXPORT 
#endif


class RSBLOCK_EXPORT RsBlockEncoder : public CF__Resource_impl {
public: 
  static status
  Create (RsBlockEncoder *& handle,
	  const char * identifier);

  status Run ();
  static void RunWorker (void * param);
  status RunLoop ();

  // From CF::Resource
  void start ()
    throw (CORBA::SystemException, CF::Resource::StartError);
  void stop ()
    throw (CORBA::SystemException, CF::Resource::StopError);

  // From CF::PortSupplier
  CORBA::Object_ptr getPort (const CORBA::Char * name)
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

private:
  RsBlockEncoder (const char *identifier);
  ~RsBlockEncoder ();

  status Init ();

  // Port definitions.
  RsBlockInPort_impl * m_rsblockin;
  RsBlockOutPort_impl * m_rsblockout;

  // Used with file read.
  unsigned int m_input_count;
  unsigned int m_samples_read;
  char * m_input;

  unsigned int m_output_count;
  char * m_output;

  bool m_bStop;
  bool m_threadrunning;
  SCE::OS::ThreadManager m_thread;

  unsigned int m_n, m_k, m_N, m_K, m_m, m_b, m_t;
  galois::GaloisField * m_gf;
  galois::GaloisFieldPolynomial * m_genpoly;
  galois::GaloisFieldPolynomial * m_encMsg;
};


class RSBLOCK_EXPORT RsBlockDecoder : public CF__Resource_impl {
public:
  static status
  Create (RsBlockDecoder *& handle,
	  const char * identifier);

  status Run ();
  static void RunWorker (void * param);
  status RunLoop ();
  void SendFeedback (SDR_FM3TR::MAC_Signaling::MAC_Signal_Ack ack);

  // From CF::Resource
  void start ()
    throw (CORBA::SystemException, CF::Resource::StartError);
  void stop ()
    throw (CORBA::SystemException, CF::Resource::StopError);

  // From CF::PortSupplier
  CORBA::Object_ptr getPort (const CORBA::Char * name)
    throw (CORBA::SystemException, CF::PortSupplier::UnknownPort);

private:
  RsBlockDecoder (const char *identifier);
  ~RsBlockDecoder ();

  status Init ();

  // Port definitions
  RsBlockInPort_impl * m_rsblockin;
  RsBlockOutPort_impl * m_rsblockout;

  // MAC Interface definitions
  MAC_Ctrl_OutPort_impl<SDR_FM3TR::MAC_Signaling::MAC_ErrorControl_ReceiveLocal> * m_mac_err_out;
  MAC_Ctrl_OutPort_impl<SDR_FM3TR::MAC_Signaling::MAC_ErrorControl_ReceiveLocal> * m_mac_err_out2;

  char * m_input;
  char * m_output;
  short * m_mac_output; // Medium access control output port buffer.

  bool m_bStop;
  bool m_threadrunning;
  SCE::OS::ThreadManager m_thread;

  unsigned int m_n, m_k, m_N, m_K, m_m, m_b, m_t;
  galois::GaloisField * m_gf;
  galois::GaloisFieldPolynomial * m_decMsg;

  unsigned int m_numPackets;
  unsigned int m_errorThreshold;
};

#endif /* RSBLOCK_H */
